from tkinter import messagebox
from PIL import Image, ImageTk
import tkinter as tk


def save_edits_func(card_frame, new_task_name, new_priority, new_due_date, new_image_path, dialog):
    try:
        for widget in card_frame.winfo_children():
            if isinstance(widget, tk.Label) and widget.cget('text').startswith("Task:"):
                widget.config(text=f"Task: {new_task_name}")
            elif isinstance(widget, tk.Label) and widget.cget('text').startswith("Priority:"):
                widget.config(text=f"Priority: {new_priority}")
            elif isinstance(widget, tk.Label) and widget.cget('text').startswith("Due Date:"):
                widget.config(text=f"Due Date: {new_due_date}")
            elif isinstance(widget, tk.Label) and hasattr(widget, 'image'):
                if new_image_path:
                    img = Image.open(new_image_path)
                    img = img.resize((100, 100))
                    img = ImageTk.PhotoImage(img)
                    widget.config(image=img)
                    widget.image = img
        dialog.destroy()
    except Exception as e:
        messagebox.showerror("wrongly edit task", str(e))
